home *** CD-ROM | disk | FTP | other *** search
- program Comments;
-
- uses
- SysUtils,
- Classes,
- AAChrStm;
-
-
- {$IFDEF DoNotCompile}
- //some constructions to test all cases
- {a braced comment}
- (*a parenstar comment*)
- /'{not a comment}'
- /any old chars
- /(*a comment after a slash*)
- /{a comment after a slash}
- ((*a comment after an open paren*)
- (//a comment after an open paren
- ({a comment after an open paren}
- (*a parenstar comment with a * inside of it*)
- {$ENDIF}
-
-
- procedure ExtractComments(aInStm : TaaInCharStream;
- aOutStm : TaaOutCharStream);
- type
- TStates = ( {the types of states...}
- sScanNormal, {..normal scanning}
- sScanBraceComment, {..scanning comment after getting '{'}
- sScanAfterSlash, {..scanning after getting first '/'}
- sScanSlashComment, {..scanning comment after getting '//'}
- sScanAfterParen, {..scanning after getting '('}
- sScanPStarComment, {..scanning comment after getting '(*'}
- sScanAfterStar, {..scanning after getting '*' in (**) comment}
- sScanString); {..scanning a string}
- var
- State : TStates;
- Ch : char;
- begin
- State := sScanNormal;
- Ch := aInStm.GetChar;
- while (Ch <> #0) do begin
- case State of
- sScanNormal :
- begin
- case Ch of
- '''': State := sScanString;
- '(' : State := sScanAfterParen;
- '/' : State := sScanAfterSlash;
- '{' : begin
- aOutStm.PutChar('{');
- State := sScanBraceComment;
- end;
- end;{case}
- end;
- sScanBraceComment :
- begin
- aOutStm.PutChar(Ch);
- if (Ch = '}') then begin
- aOutStm.PutChar(#10);
- State := sScanNormal;
- end;
- end;
- sScanAfterSlash :
- begin
- case Ch of
- '''': State := sScanString;
- '(' : State := sScanAfterParen;
- '/' : begin
- aOutStm.PutChar('/');
- aOutStm.PutChar('/');
- State := sScanSlashComment;
- end;
- '{' : begin
- aOutStm.PutChar('{');
- State := sScanBraceComment;
- end;
- else
- State := sScanNormal;
- end;{case}
- end;
- sScanSlashComment :
- begin
- aOutStm.PutChar(Ch);
- if (Ch = #10) then
- State := sScanNormal;
- end;
- sScanAfterParen :
- begin
- case Ch of
- '''': State := sScanString;
- '(' : State := sScanAfterParen;
- '*' : begin
- aOutStm.PutChar('(');
- aOutStm.PutChar('*');
- State := sScanPStarComment;
- end;
- '/' : State := sScanAfterSlash;
- '{' : begin
- aOutStm.PutChar('{');
- State := sScanBraceComment;
- end;
- else
- State := sScanNormal;
- end;{case}
- end;
- sScanPStarComment :
- begin
- aOutStm.PutChar(Ch);
- if (Ch = '*') then
- State := sScanAfterStar;
- end;
- sScanAfterStar :
- begin
- aOutStm.PutChar(Ch);
- if (Ch = ')') then begin
- aOutStm.PutChar(#10);
- State := sScanNormal
- end
- else
- State := sScanPStarComment;
- end;
- sScanString :
- begin
- if (Ch = '''') then
- State := sScanNormal;
- end;
- end;{case}
- Ch := aInStm.GetChar;
- end;
- end;
-
-
- var
- InStm : TFileStream;
- OutStm : TFileStream;
- InChStm : TaaInCharStream;
- OutChStm : TaaOutCharStream;
- Ch : char;
- SmallBuf : array [0..19] of byte;
- begin
- InStm := nil;
- InChStm := nil;
- OutStm := nil;
- OutChStm := nil;
- try
- InStm := TFileStream.Create('comments.dpr', fmOpenRead+fmShareDenyNone);
- InChStm := TaaInCharStream.Create(InStm);
- OutStm := TFileStream.Create('comments.out', fmCreate);
- OutChStm := TaaOutCharStream.Create(OutStm);
-
-
- ExtractComments(InChStm, OutChStm);
-
- finally
- OutChStm.Free;
- OutStm.Free;
- InChStm.Free;
- InStm.Free;
- end;
- end.
-
-